package com.nike.amoeba.ui.frg

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.kingja.loadsir.core.LoadService
import com.nike.amoeba.R
import com.nike.amoeba.adapter.StockAdjListAdapter
import com.nike.amoeba.base.BaseFragment
import com.nike.amoeba.databinding.FStockAdjListBinding
import com.nike.amoeba.ext.STOCK_ADJ_ORDER_TYPE
import com.nike.amoeba.ext.init
import com.nike.amoeba.ext.loadServiceInit
import com.nike.amoeba.ext.setNbOnItemClickListener
import com.nike.amoeba.ext.showEmpty
import com.nike.amoeba.ext.showLoading
import com.nike.amoeba.mar.eventViewModel
import com.nike.amoeba.viewmodel.RfidModel
import com.nike.amoeba.viewmodel.StockAdjustModel
import com.nike.amoeba.weight.recyclerview.SpaceItemDecoration

/**
 * User: Nike
 *  2024/5/28 10:25
 *      库存详细调整
 */
class StockAdjListFrg: BaseFragment<StockAdjustModel, FStockAdjListBinding>() {

    val adapter: StockAdjListAdapter by lazy { StockAdjListAdapter(arrayListOf()) }

    lateinit var loadsir: LoadService<Any>

    val rfidModel: RfidModel by activityViewModels()

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mDatabind.click = ProxyClick()
        arguments?.let {
            mViewModel.assetNo.set(it.getString("assetNo"))
            mViewModel.orderId.set(it.getString("orderId"))
            mViewModel.code.set(it.getString("code"))
        }
        setToolBarTitle(getString(R.string.number) + "：" + mViewModel.assetNo.get())

        //状态页配置
        loadsir = loadServiceInit(mDatabind.swipeRefresh) {
            lazyLoadData()
        }

        //初始化recyclerView
        mDatabind.recyclerView.init(LinearLayoutManager(context), adapter).let {
            it.addItemDecoration(SpaceItemDecoration(ConvertUtils.dp2px(20f), ConvertUtils.dp2px(20f), true))
        }

        adapter.run {
            setNbOnItemClickListener{adapter, view, position ->

            }
        }

        mDatabind.swipeRefresh.isRefreshing = false
        mDatabind.swipeRefresh.setOnRefreshListener {
            mViewModel.onInventoryDetail(mViewModel.orderId.get(), mViewModel.code.get())
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.listBean.observe(viewLifecycleOwner, Observer {
            if (it.isSuccess)loadsir.showSuccess() else loadsir.showEmpty()
            adapter.setList(it.listData)
            adapter!!.appendList(it.listData)
            mDatabind.layout.visibility = if (it.listData.size != 0) View.VISIBLE else View.GONE
            mDatabind.swipeRefresh.isRefreshing = false
        })
        //扫码
        eventViewModel.zkingType.observeInFragment(this, Observer {
            if (it.Type == STOCK_ADJ_ORDER_TYPE){
                rfidModel.scanData.value = it.text
            }
        })
        rfidModel.scanData.observe(viewLifecycleOwner, { scanData ->
            if (scanData != null && viewLifecycleOwner.lifecycle.currentState == Lifecycle.State.RESUMED) {
                val bean = adapter.data.firstOrNull { it.OrderNo == scanData }
//                bean?.let { UIHelper.startStockListFrg(nav(), it.OrderNo!!) }
            }
        })
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        //设置界面 加载中
        loadsir.showLoading()
        mViewModel.onInventoryDetail(mViewModel.orderId.get(), mViewModel.code.get())
    }

    inner class ProxyClick() {

        fun save(){
            mViewModel.onUpload(adapter.mFilterList, mViewModel.orderId.get(), this@StockAdjListFrg, mViewModel.code.get())
        }
    }

}